package edu.zut.cs.software.cms.module.score.web.spring.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.zut.cs.software.cms.base.web.spring.controller.GenericController;
import edu.zut.cs.software.cms.module.score.domain.Score;
import edu.zut.cs.software.cms.module.score.service.ScoreManager;
/**
 * 
 * @author admix
 *
 */

@Controller
@RequestMapping("/score")
@ResponseBody
public class ScoreController extends GenericController<Score, Long, ScoreManager> {

	ScoreManager scoreManager;

	@RequestMapping(method = RequestMethod.GET, value = "/index.html")
	public String index() {
		String result = "/score/index";
		return result;
	}

	@Autowired
	public void setScoreManager(ScoreManager scoreManager) {
		this.scoreManager = scoreManager;
		this.manager = this.scoreManager;
	}
	
	/*
	 * @RequestMapping("/deleteOneScore")
	 * 
	 * @ResponseBody public boolean deleteOneScore(long id) { Boolean result =
	 * this.manager.deleteById(id); return result; }
	 */

	@GetMapping("/deletebyid/{id}")
	public Boolean deleteById(@PathVariable("id") Long id) {
		Boolean result = this.manager.deleteById(id);
		return result;
	}
	
	
	@RequestMapping("/modifyOneScore")
	@ResponseBody
	public String modifyOneScore(long id,String usual_score,String final_score,String total_score) {
		if(scoreManager.modifyById(id, usual_score, final_score, total_score)==true)
			return "success!";
		else
			return "failed!";
	}
	@RequestMapping("/RetrieveById")
	@ResponseBody
	public Score RetrieveById(long id) {
		Score score=new Score();
		score=scoreManager.findById(id);
		return score;
	}

	
	@RequestMapping("/CreateOne")
	@ResponseBody
	public String CreateOneScore(long id,String cid,String clazz,String cname,String sid,String sname) {
		String flag="";
		Score score=new Score();
		if(scoreManager.IfExist(cid))//返回true
		{
			flag="true";
			score.setCid(cid);
			score.setClazz(clazz);
			score.setCname(cname);
			score.setSid(sid);
			score.setSname(sname);
		}
		else {
			flag="false";
		}
		return flag;
	}

}

